﻿/*
 * ----------------------------------------------------------------------------
 *
 *  Vana'Diel clock Plugin for Google Desktop Sidebar.
 *
 *  (c) 2006, Tennetiss some rights reserved.
 *  http://www.akaiho.com/
 *
 *  Final Fantasy XI is originally created by SQUARE ENIX CO. LTD.
 *  C) 2005 SQUARE ENIX CO., LTD. All Rights Reserved (FF XI).
 *
 *  CHANGELOG:
 *    2006-02-09  :  initially setup
 *    2006-02-12  :  tile title shows the time when minimized
 *    2006-02-28  :  Bugs fixed
 *    2006-03-02  :  bug-fix
 *    2007-06-15  :  refactored for the newer APIs
 *
 * ----------------------------------------------------------------------------
 */

var kCurrentVersion = "1.20";

// Gloval variables
var GvWeek, GvMoon;
var GvfWeek, GvfMoon;
var theTime;
var tz_jp = 9 * 60; // GMT+9


// original VanaTime
var originalVanaTime = (903*60*60*24*30*12 + (12-1)*60*60*24*30 + (21-1)*60*60*24 + 1*60*60 + 56*60)*1000;

//------------------------------------------
// graphics for days
var vWeeks;
var weeksImage = new Array( "week01.gif", "week02.gif" ,"week03.gif", "week04.gif",
"week05.gif", "week06.gif", "week07.gif", "week08.gif");


//------------------------------------------
// moon phase 
var vTextMoons;
var moonImage = new Array( "m10.gif", "m11.gif", "m00.gif", "m01.gif", "m02.gif", "m03.gif",
  "m04.gif", "m05.gif", "m06.gif", "m07.gif", "m08.gif", "m09.gif");
  
var kClockInterval; // refresh
var kDaylight;
var kTimerId;
var isMinimized;


var tz_offset;
var pos_start;
var pos_end;

var contentItems = new Array();
//var gItem_clock = new ContentItem();
//var gItem_moon = new ContentItem();
//var gItem_week = new ContentItem();
var gItem_allImages = new ContentItem();

//var gItem_clock

// --------------------------------------------------------

Main();

function Main() {

  SetDefaultOptions();
  Recalculate();
    
//  pluginHelper.SetFlags(gddPluginFlagNone, gddContentFlagManualLayout );
//  pluginHelper.about_text = aboutText;
//  pluginHelper.max_content_items = 5;
  pluginHelper.onDisplayStateChange = Set_graphics;

//  pluginHelper.onAddCustomMenuItems = AddCustomMenuItems;
  pluginHelper.onShowOptionsDlg = ShowOptionsDlg;

  Init_item();
  kTimerId = utils.setInterval(printFFXITime, kClockInterval*1000);
//  pluginHelper.AddContentItem(gItem_clock, gddItemDisplayInSidebar);
//  pluginHelper.AddContentItem(gItem_week, gddItemDisplayInSidebar);
//  pluginHelper.AddContentItem(gItem_moon, gddItemDisplayInSidebar);
}


function Init_item(){

  kClockInterval = options.GetValue("ClockInterval");
  
  kDaylight = options.GetValue("IsDaylight");

  if (options.GetValue("IsMinimized") == 1){ isMinimized = true; }
  else { isMinimized = false; }
/*
  gItem_clock.layout = gddContentItemLayoutNowrapItems;
  gItem_clock.source = "";
  gItem_clock.tooltip = "";
  gItem_clock.snippet = "";
  gItem_clock.flags = gddContentItemFlagStatic;
  gItem_clock.SetRect(3,2, 110, 16);
  contentItems.push(gItem_clock);
  GvWeek = -1;
  GvMoon = -1;

  gItem_week.layout = gddContentItemLayoutNowrapItems;
  gItem_moon.layout = gddContentItemLayoutNowrapItems;
  gItem_week.flags = gddContentItemFlagStatic;
  gItem_moon.flags = gddContentItemFlagStatic;
  */
  Set_graphics();
}

function Set_graphics(newState){
  if(newState == gddTileDisplayStateMinimized){
    isMinimized = true;
    options.PutValue("IsMinimized", 1);

  }
  else if(newState == gddTileDisplayStateRestored){
    isMinimized = false;
//    pluginHelper.title = "Vana\'Diel Clock";
    options.PutValue("IsMinimized", 0);
    GvWeek = -1;
    GvMoon = -1;
  }
/*
  gItem_week.SetRect( pluginHelper.window_width - 18, 0, 16, 16 );
  gItem_moon.SetRect( pluginHelper.window_width - 18 - 18, 2, 12, 12 );
  */
  Recalculate();
  
}


function nowat(now_t, tz, st) {
  var hour, min, sec;
  
  var pos_t = now_t + (tz_offset + tz) * 60 * 1000;
  
  if (0 != st) {
    // サマータイム処理
    if ((pos_start >= pos_t) || (pos_end < pos_t)) {
      pos_t -= 60 * 60 * 1000;
    }
  }

  var t = new Date();
  t.setTime(pos_t);
  return t;
}

function SetDefaultOptions() {
  if (options.GetValue("IsMinimized") == "") {
    options.PutValue("IsMinimized", 0);
  }
  if (options.GetValue("ClockInterval") == "") {
    options.PutValue("ClockInterval", 2);
  }
  if (options.GetValue("IsDaylight") == "") {
    options.PutValue("IsDaylight", 0);
  }
}


function Recalculate() {

  var today = new Date();
  var year = today.getYear();
  if (year < 100) {
    year += 1900;
  }
  
  tz_offset = today.getTimezoneOffset();
  
  pos_start = GetDstStart(year);
  pos_end = GetDstEnd(year);
}


// ---- Options Dialog ----

function ShowOptionsDlg(wnd) {
  var checkBox = wnd.AddControl(gddWndCtrlClassButton, gddWndCtrlTypeButtonCheck, "isDaylight", "",
                 15, 13, 10, 10);
  if( options.GetValue("IsDaylight") == 1 ){ checkBox.value = true; }
  else { checkBox.value = false; }
  wnd.AddControl(gddWndCtrlClassLabel, 0, "", ENTER_DAYLIGHTSAVING,
                 35, 10, 170, 30);

//  checkBox.value = options.GetValue("IsDaylight");

  wnd.AddControl(gddWndCtrlClassLabel, 0, "", ENTER_CLOCK_INTERVAL,
                 35, 63, 120, 15);
  wnd.AddControl(gddWndCtrlClassEdit, 0, "clockInterval", options.GetValue("ClockInterval"),
                 170, 60, 40, 22);


  wnd.onClose = OptionsDlgClosed;
}


function OptionsDlgClosed(wnd, code) {
  if (code == gddIdOK) {

    var clockinterval = wnd.GetControl("clockInterval");
    if( isNaN(clockinterval.text) || (clockinterval.text <= 0) ){
      utils.alert( INPUT_TIME_ERROR );
    }
    else {
      options.PutValue("ClockInterval", clockinterval.text);
      kClockInterval = clockinterval.text;

      Recalculate();
      utils.clearInterval(kTimerId);
      kTimerId = utils.setInterval(printFFXITime, kClockInterval*1000);

    
      var isdaylight = wnd.GetControl("isDaylight");
//    options.PutValue("IsDaylight", isdaylight.value);
      if( isdaylight.value == true ){
        kDaylight = 1;
        options.PutValue("IsDaylight", 1);
      }
      else{
        kDaylight = 0;
        options.PutValue("IsDaylight", 0);
      }
    }

  }
}

function view_onminimize(){
  isMinimized = true;
}

function view_onrestore(){
  isMinimized = false;
}
//----------------
//---------------=

function printFFXITime() {
//------------------------------------------

  nowTime = new Date();
  nowTime = nowTime.getTime();
  
  // 元の地球時間
  var Earth_oDateTime = new Date( 2002, 8,16, 19,16,40  );
//  Earth_oDateTime = Earth_oDateTime.getTime();
  
  // 現在の地球時間(local)
  Earth_nDateTime = new Date();
  // 地球時間を秒に変換
  var EarthOrigin = Earth_oDateTime;
  var tmp_Earth = new Date();
  var now = new Date();
  var n_t = now.getTime();
  var JPNow = nowat(n_t, tz_jp, kDaylight);

//------------------------------------------
  // 元のヴァナ時間
  var VanaOrigin = originalVanaTime;
  var VanaOrigin2 = VanaOrigin;
  // 現在のヴァナ時間
  var VanaNow = VanaOrigin + ( (JPNow - EarthOrigin) * 25 );
//------------------------------------------
  // 現在のヴァナ曜日
  var vWeek = Math.floor( VanaNow / (1000*60*60*24) ) % 8;
//------------------------------------------
  // 現在のヴァナ月齢
  var vMoon = Math.floor( (VanaNow + 1000 * 24 * 60 * 60 * 2) / (1000 * 24 * 60 * 60 * 7) ) % 12;
//------------------------------------------
  // ヴァナ vSec 秒を各要素に変換
  // 年
  var vYy = Math.floor( VanaNow / (1000 * 360 * 24 * 60 * 60) );
    VanaNow = VanaNow % (1000 * 360 * 24 * 60 * 60);
  // 月
  var vMm = Math.floor( VanaNow / (1000 * 30 * 24 * 60 * 60) + 1 );
    VanaNow = VanaNow % (1000 * 30 * 24 * 60 * 60);
  // 日
  var vDd = Math.floor( VanaNow / (1000 * 24 * 60 * 60) + 1 );
    VanaNow = VanaNow % (1000 * 24 * 60 * 60);
  // 時
  var vHh = Math.floor( VanaNow / (1000 * 60 * 60) );
    VanaNow = VanaNow % (1000 * 60 * 60);
  // 分
  var vNn = Math.floor( VanaNow / (1000 * 60) );
    VanaNow = VanaNow % (1000 * 60);
  // 秒
  var vSs = Math.floor( VanaNow / 1000 );
  
  // to format
  if (vYy < 1000) { vYy = '0' + vYy; }
  if (vMm <   10) { vMm = '0' + vMm; }
  if (vDd <   10) { vDd = '0' + vDd; }
  if (vHh <   10) { vHh = '0' + vHh; }
  if (vNn <   10) { vNn = '0' + vNn; }
  if (vSs <   10) { vSs = '0' + vSs; }
  


//utils.alert( eYy + '/' + eMm + '/' + eDd + ' ' + eHh + ':' + eNn + ':' + eSs );

//  gItem_clock.heading = vYy + '/' + vMm + '/' + vDd + ' ' + vHh + ':' + vNn + ':' + vSs;
  gItem_clock.innerText = vYy + '/' + vMm + '/' + vDd + ' ' + vHh + ':' + vNn + ':' + vSs;
  
  // 曜
  if (GvWeek != vWeek) {
    gItem_week.src = weeksImage[vWeek];
  }
  // 月
  if (GvMoon != vMoon) {
    gItem_moon.src = moonImage[vMoon];
  }

  if(isMinimized == true){
    caption = vMm + '/' + vDd + ' ' + vHh + ':' + vNn + ' ' + vWeekText[vWeek] + ' ' + vMoonText[vMoon];
  }
  else { caption = "Vana'Diel Clock"; }
  
  // Save the values globally
  GvWeek   = vWeek;
  GvMoon   = vMoon;

}


function GetDstStart(arg_year) {
  var dst_start = new Date(arg_year, 3, 1, 2, 0, 0); // 第2引数の3は、4月を表す。
  for (var i = 1; i <= 7; i++) {
    dst_start.setDate(i);
    if (0 == dst_start.getDay()) {
      break;
    }
  }
  return dst_start.getTime();
}

// 引数arg_yearで指定された年のサマータイム終了時刻を内部表現で返す。
//
function GetDstEnd(arg_year) {
  var dst_end = new Date(arg_year, 9, 31, 1, 0, 0);
  for (var i = 31; i > 24; i--) {
    dst_end.setDate(i);
    if (0 == dst_end.getDay()) {
      break;
    }
  }
  return dst_end.getTime();
}


//==========================================
// -->